package k8s_ui

import (
	"fmt"
	"github.com/dgrijalva/jwt-go"
	"k8s-platform/config"
	"log"
	"time"
)

var Login login

type login struct{}

func (l *login) Auth(username, password string) (msg int64, data string, err error) {

	jenkinsConfigTest := &config.JenkinsConfigTest{}
	mysqlconfig, err := jenkinsConfigTest.ConfigTest()
	if err != nil {
		log.Fatalf("Failed to parse configuration: %v", err)
	}

	a := mysqlconfig.AdminUser
	b := mysqlconfig.AdminPwd
	fmt.Printf("a: %s, b: %s\n", a, b)

	//未做多用户登入功能，临时配置文件登入
	if username == mysqlconfig.AdminUser && password == mysqlconfig.AdminPwd {
		token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
			"exp": time.Now().Add(time.Hour * 72).Unix(),
			"iat": time.Now().Unix(),
			"sub": username,
		})

		//返回token给前端加入了自定义字符串
		tokenString, err := token.SignedString([]byte("luweiqq444985876"))
		if err != nil {
			panic(err.Error())
		}
		return 200, tokenString, err

	} else {
		return 500, "nil", err
	}
}

//func (l *login) Auth(username, password string) (err error) {
//	jenkinsConfigTest := &config.JenkinsConfigTest{}
//	mysqlconfig, err := jenkinsConfigTest.ConfigTest()
//	if username == mysqlconfig.AdminUser && password == mysqlconfig.AdminPwd {
//		//if username == config.AdminUser && password == config.AdminPwd {
//		return nil
//	} else {
//		logger.Error("登录失败，用户名或密码错误")
//		return errors.New("登录失败，用户名或密码错误")
//	}
//
//	return nil
//}
